<?php
/**
 * Yotaku shop - yshop
 * Copyright (c) 2016 http://www.yotaku.cn All rights reserved.
 * Author: Creatint <creatint@163.com> <http://creatint.yotaku.cn>
 */
namespace Admin\Model;
use Think\Model;

class ArticleModel extends Model{
    protected $error = '';
    /* 用户模型自动验证 */
    protected $_validate = array(
        /* 验证name */
        array('name', '', '标识已存在', self::EXISTS_VALIDATE,'unique'),

        /* 验证title */
        array('title', 'require', '标题不能为空', self::MUST_VALIDATE),
        array('title', '1,30', '标题长度为1-30个字符', self::MUST_VALIDATE,'length'),

        /* 验证desc */
        array('desc', '1,100', '描述信息长度为1-100个字符', self::EXISTS_VALIDATE, 'length'),

        //验证content
        array('content','0,3000',-4,self::MUST_VALIDATE,'length'),

    );
    


    public function getError(){
        return $this->error;
    }
    public function getLists(){
        $map['status'] = 1;
        if($result = $this->where($map)->select()){
            return $result;
        }else{
            $this->error    =   'ID不存在';
            return false;
        }
    }

    public function getDetail($id){
        if(is_numeric($id)){
            $map['id']  =   $id;
        }
        $map['status'] = 1;
        if($result = $this->where($map)->find()){
            return $result;
        }else{
            $this->error    =   'ID不存在';
            return false;
        }
    }


    public function update($data=null){
        if(!$data){
            $this->error = '数据为空';
            return false;
        }
        $data = $this->create($data);

        if($data['id']){
            $this->where("id={$data['id']}")->save($data);
        }else{
            $id = $this->add();
            if(!$id){
                $this->error = '新增失败';
                return false;
            }
        }
        return $data;
    }
}